<?php

namespace app\admin\controller;

use app\admin\model\HomePage;
use app\common\Cache\HomePageCache;

class HPage extends AdminBase
{
    //
    public function getList()
    {
        $data = HomePage::getInstance()->getList([], '*');
        foreach ($data as &$v) {
            if ($v['status'] == 0) {
                $v['statusName'] = "关闭";
            } else {
                $v['statusName'] = "启用";
            }
        }
        $this->assign("data", $data);
        return $this->fetch();
    }

    /**
     * 添加启动页图片
     * @return mixed
     */
    public function add()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['img_url'])) {
                $this->error('启动页图片不可为空');
            }
            if (empty($input['outer_url'])) {
                $this->error('外链不可为空');
            }
            $insert_data = array(
                'img_url' => $input['img_url'],
                'outer_url' => $input['outer_url'],
                'status' => $input['status'],
                'sort' => $input['sort'],
                'add_time' => date("Y-m-d H:i:s"),
            );
            $res = HomePage::getInstance()->insert($insert_data);
            if ($res > 0) {
                HomePageCache::delHomePageCache();
                $this->success('添加成功', 'getList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            return $this->fetch();
        }
    }

    public function update()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['img_url'])) {
                $this->error('启动页图片不可为空');
            }
            if (empty($input['outer_url'])) {
                $this->error('外链不可为空');
            }
            $s_data = array(
                'img_url' => $input['img_url'],
                'outer_url' => $input['outer_url'],
                'status' => $input['status'],
                'sort' => $input['sort'],
                'add_time' => date("Y-m-d H:i:s"),
            );
            $res = HomePage::getInstance()->where(['id' => $input['id']])->update($s_data);
            if ($res > 0) {
                HomePageCache::delHomePageCache();
                $this->success('更新成功', 'getList');
            } else {
                $this->error('更新失败，请重试');
            }
        } else {
            //加载页面
            $input = input();
            $where = ['id' => $input['id']];
            $info = HomePage::getInstance()->getOne($where);
            $this->assign("data", $info);
            return $this->fetch();
        }
    }

    public function updateStatus()
    {
        $id = input("param.id");
        $status = input('param.status');
        if ($status == 1) {
            $update_status = 0;
        } else {
            $update_status = 1;
        }
        $info = HomePage::getInstance()->where(['id' => $id])->update(['status' => $update_status]);
        if ($info > 0) {
            HomePageCache::delHomePageCache();
            $this->success('更新成功', 'getList');
        } else {
            $this->error('更新失败，请重试');
        }
    }

    /**
     * 删除启动页图片
     * @throws \Exception
     */
    public function del()
    {
        if (request()->isPost()) {
            $id = input("param.id");
            $info = HomePage::getInstance()->where(['id' => $id])->delete();
            if ($info > 0) {
                HomePageCache::delHomePageCache();
                $this->success('删除成功', 'getList');
            } else {
                $this->error('删除失败，请重试');
            }
        }
    }
}
